﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using Fisharoo.Common;
using Fisharoo.DataAccess;
using Fisharoo.DataAccess.Interfaces;
using Fisharoo.Interfaces;

namespace Fisharoo.Components
{
    [Export(typeof(IAccountService))]
    public class AccountService : IAccountService
    {
        [Import]
        private IAccountRepository _accountRepository;
        [Import]
        private IPermissionRepository _permissionRepository;
        [Import]
        private IUserSession _userSession;
        [Import]
        private IRedirector _redirector;
        [Import]
        private IEmail _email;

        public AccountService()
        {
            MEFManager.Compose(this);
        }

        public bool UsernameInUse(string Username)
        {
            Account account = _accountRepository.GetAccountByUsername(Username);
            if (account != null)
                return true;

            return false;
        }

        public bool EmailInUse(string Email)
        {
            Account account = _accountRepository.GetAccountByEmail(Email);
            if (account != null)
                return true;

            return false;
        }

        public void Logout()
        {
            _userSession.LoggedIn = false;
            _userSession.CurrentUser = null;
            _userSession.Username = "";
            _redirector.GoToAccountLoginPage();
        }

        public string Login(string Username, string Password)
        {
            Password = Password.Encrypt(Password);
            Account account = _accountRepository.GetAccountByUsername(Username);

            //if there is only one account returned - good
            if (account != null)
            {
                //password matches
                if (account.Password == Password)
                {
                    //TODO: for development disabling email validation feature
                    //if (account.EmailVerified)
                    {
                        _userSession.LoggedIn = true;
                        _userSession.Username = Username;
                        _userSession.CurrentUser = GetAccountByID(account.AccountID);
                        _redirector.GoToHomePage();
                    }
//                    else
//                    {
//                        _email.SendEmailAddressVerificationEmail(account.Username, account.Email);
//                        return @"The login information you provided was correct 
//                                but your email address has not yet been verified.  
//                                We just sent another email verification email to you.  
//                                Please follow the instructions in that email.";
//                    }
                }
                else
                {
                    return "Your Password seems to be incorrect. Try again!";
                }
            }

            return "Check your Username and try again!";
        }


        public Account GetAccountByID(Int32 AccountID)
        {
            Account account = _accountRepository.GetAccountByID(AccountID);
            List<Permission> permissions = _permissionRepository.GetPermissionsByAccountID(AccountID);
            foreach (Permission permission in permissions)
            {
                account.AddPermission(permission);
            }

            return account;
        }
    }
}
